
<script>
    function builderTableEvent(resData, myTable, table, layer, form){
         //使用了表单元素事件 如switch
         form.on('switch(table-switch)', function (obj) {
            //开启限制频次- start
            var requestTime = 1000;
            var idField = obj.elem.getAttribute('data-id') + "_" + obj.elem.getAttribute('data-field');
            var historyRequestUrl = store.get('id_field');
            var lastRequestTime = store.get('id_field_last_request_time')
             if (lastRequestTime > (new Date().getTime())) {
                 if (historyRequestUrl == idField) {
                     layer.msg(parseInt(requestTime / 1000) + "秒内同一条数据只允许更新一次", function () { });
                     //更新失败时还原值
                     obj.elem.checked = !obj.elem.checked;
                     form.render();
                     //实时更新switch组件上的data-value值
                     if (obj.elem.checked) {
                         obj.elem.setAttribute('data-value', obj.elem.getAttribute('data-on'));
                     }
                     if (!obj.elem.checked) {
                         obj.elem.setAttribute('data-value', obj.elem.getAttribute('data-off'));
                     }
                     return false;
                 }
             }
             store.set('id_field', idField);
             store.set('id_field_last_request_time', new Date().getTime() + requestTime);
             //开启限制频次- end

            // console.log(obj.elem.checked, obj.elem.getAttribute('data-id'), obj.elem.getAttribute('data-field'), obj.elem.getAttribute('data-value'));
             obj.elem.checked = obj.elem.checked;
             form.render();
             //实时更新switch组件上的data-value值
             if (obj.elem.checked) {
                 obj.elem.setAttribute('data-value', obj.elem.getAttribute('data-on'));
             }
             if (!obj.elem.checked) {
                 obj.elem.setAttribute('data-value', obj.elem.getAttribute('data-off'));
             }
       
            // console.log({ 'field': obj.elem.getAttribute('data-field'), 'value': obj.elem.getAttribute('data-value'), 'id': obj.elem.getAttribute('data-id') });
            // return false;
            var http = new Request({ token_url: api.token_url, async: false, token_store_name: 'table_switch' })
            var res = http.request('post', resData.update_field_url, { 'field': obj.elem.getAttribute('data-field'), 'value': obj.elem.getAttribute('data-value'), 'id': obj.elem.getAttribute('data-id') });
            if (res.code == 0) {
                layer.msg('更新成功');
                return true;
            }

            layer.msg(res.msg, function () { });
            //更新失败时还原值
            obj.elem.checked = !obj.elem.checked;
            form.render();
            //实时更新switch组件上的data-value值
            if (obj.elem.checked) {
                obj.elem.setAttribute('data-value', obj.elem.getAttribute('data-on'));
            }
            if (!obj.elem.checked) {
                obj.elem.setAttribute('data-value', obj.elem.getAttribute('data-off'));
            }
            return false;
        });

   

        //表格内置--编辑事件
        table.on('edit(myTable)',function(obj){
            var http = new Request({ token_url: api.token_url, async: false, token_store_name: 'table_edit' })
            var res = http.request('post',resData.update_field_url,{'field': obj.field,'value':obj.value,'id':obj.data.id});

            if(res.code == 0){
                layer.msg('更新成功');
                return true;
            }

            layer.msg(res.msg,function(){});
            return false;
        })

        //
        table.on('tool(myTable)', function (obj) {
            // console.log(obj);
            var data = obj.data; //获得当前行数据
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            obj.update(obj.data);
            form.render();
            switch(obj.event){
                //操作区域 的 编辑事件 按钮
                case 'table_edit':
                    // console.log(resData.right_edit_url);
                    // location.hash = resData['right_edit_url'];
                  
                    var editUrl = '';
                    if(resData['right_edit_url_param_field_value'] != ''){
                        data[resData['right_edit_url_param_field']] = resData['right_edit_url_param_field_value'];
                    }
                    if(resData['right_edit_url_param_extra_field_value'] != ''){
                        data[resData['right_edit_url_param_extra_field']] = resData['right_edit_url_param_extra_field_value'];
                    }
                    if(resData['is_hash_edit']){
                        //如果是hash 将以当前的链接作为基础链接 
                        editUrl += resData['base_url'];
                        if(resData['right_edit_url_param_field'] != '' && data[resData['right_edit_url_param_field']] != undefined){
                            editUrl += "?" + resData['right_edit_url_param_field']+"="+data[resData['right_edit_url_param_field']];
                        }
                        if(resData['right_edit_url_param_extra_field'] != '' && data[resData['right_edit_url_param_extra_field']] != undefined){
                            if(editUrl.indexOf('?')>0){
                                editUrl += "&"+resData['right_edit_url_param_extra_field']+"="+data[resData['right_edit_url_param_extra_field']];
                            }else{
                                editUrl += "?"+resData['right_edit_url_param_extra_field']+"="+data[resData['right_edit_url_param_extra_field']];
                            }
                        }
                        editUrl += (resData['right_edit_url'].substr(0, 1) == "#" ? resData['right_edit_url'] : "#"+resData['right_edit_url']);
                        
                    }else{
                        editUrl += resData['right_edit_url'];
                        if(resData['right_edit_url_param_field'] != '' && data[resData['right_edit_url_param_field']] != undefined){
                            editUrl += "?"+resData['right_edit_url_param_field']+"="+data[resData['right_edit_url_param_field']];
                        }
                        if(resData['right_edit_url_param_extra_field'] != '' && data[resData['right_edit_url_param_extra_field']] != undefined){
                            if(editUrl.indexOf('?')>0){
                                editUrl += "&"+resData['right_edit_url_param_extra_field']+"="+data[resData['right_edit_url_param_extra_field']];
                            }else{
                                editUrl += "?"+resData['right_edit_url_param_extra_field']+"="+data[resData['right_edit_url_param_extra_field']];
                            }
                        }

                    }
                    // console.log(editUrl);
                    window.location.href = editUrl;

                    break;
                case 'copy_field':
                    var copy_field_str = '';
                    for(var i=0 ;i<resData.right_buttons.length; i++){
                        if(resData.right_buttons[i].event == 'copy_field'){
                            copy_field_str = obj.data[resData.right_buttons[i]['params']['copy_field']] == undefined?'未定义复制的字段':obj.data[resData.right_buttons[i]['params']['copy_field']];
                        }
                    }
                     var clipboard = new ClipboardJS('.copy_field', {text: function() {return copy_field_str;}}); clipboard.on('success', function(e) {layer.msg("复制成功");}); clipboard.on('error', function(e) {layer.msg("复制失败!");});
                    break;    
                case 'copy_url':
                     var clipboard = new ClipboardJS('.copy_url', {text: function() {return returnUrl(resData,obj.data,'copy_url');}}); clipboard.on('success', function(e) {layer.msg("复制成功");}); clipboard.on('error', function(e) {layer.msg("复制失败!");});
                    break;    
                case 'link':
                    var url = returnUrl(resData,obj.data,'link');
                        window.location.href = url;  
                    break;    
                //目前有问题，生成的二维码链接中不能包含“#”字符 所以不能使用hash路由    
                case 'qrcode':
                    var result = returnUrl(resData,obj.data,'qrcode');  
                        //判断是否有二维码链接
                        if(result.url != ''){
                            var html = '<style>#right_qrcode{align-items: center;display:flex;justify-content:center;height:100%;}#right_qrcode img{width:160px;height:160px;}<\/style><div id="right_qrcode"></div><script>new QRCode("right_qrcode", { text:"' + result.url + '",width: "160",height: "160",colorDark: "#000000",colorLight: "#ffffff",correctLevel: QRCode.CorrectLevel.H});<\/script>';
                            layer.open({
                                type: 1,
                                title: (result.title !== undefined && result.title !== '')?result.title:'请扫一扫二维码',
                                area: ['250px', '250px'], //宽高
                                skin: 'layui-layer-demo', //样式类名
                                closeBtn: 1, //不显示关闭按钮
                                anim: 2,
                                shadeClose: true, //开启遮罩关闭
                                content: html
                            });
                        }else{
                            layer.msg('二维码链接不能为空',function(){})
                        }    
                   
                    break;  
                //显示图片 一般都是二维码    
                case 'image':
                        var result = returnUrl(resData,obj.data,'image');  
                        //判断是否有二维码链接
                        if(result.url != ''){
                            var html = '<style>#right_qrcode{align-items: center;display:flex;justify-content:center;height:100%;}#right_qrcode img{width:160px;height:160px;}<\/style><div id="right_qrcode"><i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop" style="display: block;position:absolute;font-size:50px;"></i><img src="'+result.url+'" alt="'+result.alt+'" title="'+result.alt+'" style="display: block;position:absolute"></div>';
                            layer.open({
                                type: 1,
                                title: (result.title !== undefined && result.title !== '')?result.title:'请扫一扫二维码',
                                area: ['250px', '250px'], //宽高
                                skin: 'layui-layer-demo', //样式类名
                                closeBtn: 1, //不显示关闭按钮
                                anim: 2,
                                shadeClose: true, //开启遮罩关闭
                                content: html
                            });
                        }else{
                            layer.msg('二维码链接不能为空',function(){})
                        }    
                   
                    break;       
                //操作区域的删除按钮    
                case 'delete':
                    layer.confirm('删除的ID：'+obj.data.id, {
                                title:'是否删除当前ID的数据？',
                                area:['300px','auto'],
                                btn: ['确定', '取消'], //可以无限个按钮
                                skin: '', //样式类名
                            }, function (index, layero) {
                                // console.log();
                                var http = new Request({token_url:api.token_url,async:false,token_store_name:'delete'});
                                    res = http.request('post',resData.right_delete_url,{ids:obj.data.id});
                                    if(res.code == 0){
                                        //删除成功
                                        layer.msg('删除成功');
                                        //当开启行拖拽时，最好设置表格重载，避免删除之后的位置 空数据的影响
                                        if(resData.row_drag){
                                            myTable.reload();//可以有效避免排序的bug
                                        }else{
                                            obj.del();
                                        }
                                    }else{
                                        layer.msg('删除失败',function(){});
                                    }

                            }, function (index) {
                                //按钮【按钮二】的回调
                                layer.closeAll();
                            });
                   
                   
                    break; 
                //需要解决特殊事件    
                case 'row_drag':
                    obj.update(obj.data);
                    form.render();
                    break;            
                default:
                    console.log("事件类型未定义："+obj.event);
                    break;    
            }
        });
    }
</script>
